package cn.com.hhrcw.job;

import cn.com.hhrcw.entity.PiInfo;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.entity.SysUserDepart;
import cn.com.hhrcw.message.MechanismService;
import cn.com.hhrcw.message.entity.DepartResult;
import cn.com.hhrcw.message.entity.UserResult;
import cn.com.hhrcw.service.IPiInfoService;
import cn.com.hhrcw.service.ISysDepartService;
import cn.com.hhrcw.service.ISysUserDepartService;
import cn.com.hhrcw.service.ISysUserService;
import cn.com.hhrcw.utils.RedisLockUtils;
import cn.hutool.core.date.DateTime;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.PasswordUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @program: stdss-boot-parent
 * @description:
 * @author: gjf
 * @create: 2020-11-28 16:09
 **/
@Component("UserJobTask")
@Slf4j
public class UserJobTask implements Job {
    @Autowired
    private ISysDepartService sysDepartService;

    @Autowired
    private MechanismService mechanismService;

    @Autowired
    private ISysUserDepartService sysUserDepartService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IPiInfoService piInfoService;

    @SneakyThrows
    @Override
    public void execute(JobExecutionContext context) {
        try {
            if (RedisLockUtils.tryLock("USER_JOB_TASK", 1, TimeUnit.MINUTES)) {
                List<DepartResult> result = mechanismService.getDepartResult();
                if (CollectionUtils.isNotEmpty(result)) {
                    for (DepartResult departResult : result) {
                        String id = departResult.getId();
                        List<UserResult> resultList = mechanismService.getUserResult(id);
                        if (CollectionUtils.isNotEmpty(resultList)) {
                            for (UserResult user : resultList) {
                                SysUser sysUser = userService.getById(user.getId());
                                if (null == sysUser) {
                                    SysUserDepart query = new SysUserDepart();
                                    String userId = user.getId();
                                    String phone = user.getPhone();
                                    query.setDepId(id);
                                    query.setUserId(userId);
                                    sysUserDepartService.save(query);
                                    SysUser member = new SysUser();
                                    member.setId(userId);

                                    if (StringUtils.isBlank(phone)) {
                                        member.setUsername(user.getWorkno());
                                    } else {
                                        member.setUsername(phone);
                                    }
                                    member.setPhone(member.getUsername());
                                    String salt = RandomStringUtils.randomAlphanumeric(6);
                                    member.setSalt(salt);
                                    member.setPassword(PasswordUtil.encrypt(member.getUsername(), salt, member.getSalt()));
                                    member.setDelFlag(false);
                                    member.setStatus(1);
                                    userService.saveOrUpdate(member);
                                    //这里处理改为pi_info表信息为准
                                    PiInfo piInfo = new PiInfo();
                                    piInfo.setId(userId);
                                    piInfo.setUserId(userId);
                                    piInfo.setPhone(member.getUsername());
                                    piInfo.setUsername(member.getUsername());
                                    piInfo.setCreateTime(DateTime.now());
                                    piInfo.setCreateBy(userId);
                                    piInfo.setDelFlag(false);
                                    piInfo.setIdNumber(user.getCertificateNum());
                                    piInfo.setRoleType("2,");
                                    piInfo.setName(user.getName());
                                    piInfo.setSex(Integer.valueOf(user.getGender()));
                                    piInfo.setPhotoId("1329710097496555520");
                                    piInfoService.saveOrUpdate(piInfo);
                                }
                            }
                        }
                    }

                }
            }
        } finally {
            RedisLockUtils.unLock("USER_JOB_TASK");
        }

    }
}
